package com.hua.store.web.servlet;

import com.hua.store.domin.PageModel;
import com.hua.store.domin.Product;
import com.hua.store.service.ProductService;
import com.hua.store.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "ProductServlet", urlPatterns = "/ProductServlet")
public class ProductServlet extends BaseServlet {

    public String findProductByPid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String pid = req.getParameter("pid");
        ProductService service = new ProductServiceImpl();
        Product product = service.findProductByPid(pid);

        resp.setContentType("text/html;charset=utf-8");
        req.setAttribute("p", product);

        return "/jsp/product_info.jsp";
    }

    //findProductsByCidWithPage
    public String findProductsByCidWithPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
        String cid = req.getParameter("cid");
        int curNum = Integer.parseInt(req.getParameter("num"));
        ProductService service = new ProductServiceImpl();
        PageModel pageModel = service.findProductsByCidWithPage(cid, curNum);
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        req.setAttribute("page", pageModel);

        return "/jsp/product_list.jsp";
    }
}
